﻿using Dreamwork.Core.Infrastructure;
using DreamWork.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;

namespace DreamWork.Model
{
    public static class ModelExtensions
    {
        public static EntityTypeBuilder<TEntity> ConfigDefaultWithId<TEntity>(this EntityTypeBuilder<TEntity> thisBuilder) where TEntity : DataModel<Guid>
        {
            var tableName = typeof(TEntity).Name;
            thisBuilder.Property(ci => ci.Id)
              .ForNpgsqlUseSequenceHiLo($"{tableName}_hilo")
              .IsRequired();
            thisBuilder.HasKey(x => x.Id);
          

            return thisBuilder.ConfigDefault(tableName);
        }
        public static EntityTypeBuilder<TEntity> ConfigDefault<TEntity>(this EntityTypeBuilder<TEntity> thisBuilder) where TEntity : DataModel
        {
           return thisBuilder.ConfigDefault(typeof(TEntity).Name);
        }
        public static EntityTypeBuilder<TEntity> ConfigLittleDefault<TEntity>(this EntityTypeBuilder<TEntity> thisBuilder) where TEntity : DataModel
        {
            return thisBuilder.ConfigDefault(typeof(TEntity).Name.ToLower(DreamWorkConstrants.CultureInfo));
        }
        private static EntityTypeBuilder<TEntity> ConfigDefault<TEntity>(this EntityTypeBuilder<TEntity> thisBuilder, string tableName) where TEntity : DataModel
        {
            thisBuilder.ToTable(tableName);
            return thisBuilder;
        }

    }
}
